package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.LineUp;
import com.tianxin.workbao.workBao.bean.OutputBill;
import com.tianxin.workbao.workBao.dataProvide.OutPutBillProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/6/22.
 */

public class LoadCarDetailActivity extends BaseActivity implements UpdateListViewListener {

    private TextView lineTimeTV;
    private TextView lineUserTV;
    private TextView customerTV;
    private TextView coalNameTV;
    private TextView carNumberTV;
    private TextView driverNameTV;
    private TextView driverPhoneTV;
    private TextView lineNumberTV;

    private TextView loadTimeTV;
    private TextView loadUserTV;
    private TextView loadCarNameTV;

    private TextView billTimeTV;
    private TextView billUserTV;
    private TextView billNumberTV;
    private TextView piWeightTV;
    private TextView maoWeightTV;
    private TextView netWeightTV;
    private TextView sendCompanyTV;
    private TextView customerCompanyTV;
    private TextView billCoalNameTV;
    private TextView statusNameTV;
    private TextView outTimeTV;

    private LineUp mLineUp;
    private OutputBill mOutputBill;
    private List<OutputBill> mOutputBills;
    private String mBillnumber;
    private OutPutBillProvide mOutPutBillProvide;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loaded_detail);
        super.initStatusBar(true);

        initView();

        initEvent();

        initData();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }


    public void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle("装车详情");
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));

        lineTimeTV = findViewById(R.id.line_time);
        lineUserTV = findViewById(R.id.line_user);
        customerTV = findViewById(R.id.customer);
        coalNameTV = findViewById(R.id.coal_name);
        carNumberTV = findViewById(R.id.car_number);
        driverNameTV = findViewById(R.id.driver_name);
        driverPhoneTV = findViewById(R.id.driver_phone);
        lineNumberTV = findViewById(R.id.line_number);

        loadTimeTV = findViewById(R.id.load_time);
        loadUserTV = findViewById(R.id.load_user);
        loadCarNameTV = findViewById(R.id.load_car_name);

        billTimeTV = findViewById(R.id.bill_time);
        billUserTV = findViewById(R.id.bill_user);
        billNumberTV = findViewById(R.id.bill_number);
        piWeightTV = findViewById(R.id.pi_weight);
        maoWeightTV = findViewById(R.id.mao_weight);
        netWeightTV = findViewById(R.id.net_weight);
        sendCompanyTV = findViewById(R.id.send_company);
        customerCompanyTV = findViewById(R.id.customer_company);
        billCoalNameTV = findViewById(R.id.bill_coal_name);
        statusNameTV = findViewById(R.id.status_name);
        outTimeTV = findViewById(R.id.out_time);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getBundleExtra("lp");
        mLineUp = (LineUp) bundle.getSerializable("lineUp");
        mBillnumber = mLineUp.getBillnumber();
        //get OutputBill Information
        getOutputBill();
        bindView();
    }

    /**
     * get  OutputBill Information 获取过磅信息
     */
    private void getOutputBill() {
        mOutPutBillProvide = new OutPutBillProvide(LoadCarDetailActivity.this,this);
        mOutPutBillProvide.getOutPutBillByNumber(mBillnumber);
    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> LoadCarDetailActivity.this.finish());
    }

    @Override
    public void bindView() {
        lineTimeTV.setText(TimeUtils.parseDateTime(mLineUp.getAddtime()));
        lineUserTV.setText(mLineUp.getLineupusername());
        customerTV.setText(mLineUp.getBuycompany());
        coalNameTV.setText(mLineUp.getCoaltype());
        carNumberTV.setText(mLineUp.getCarnumber());
        driverNameTV.setText(mLineUp.getCarowner());
        driverPhoneTV.setText(mLineUp.getCarconnectphone());
        lineNumberTV.setText(mLineUp.getNumber());

        loadTimeTV.setText(TimeUtils.parseDateTime(mLineUp.getLoadtime()));
        loadUserTV.setText(mLineUp.getLoadusername());
        loadCarNameTV.setText(mLineUp.getLoadcarname());
        if (mOutputBill != null) {
            billTimeTV.setText(TimeUtils.parseDateTime(mOutputBill.getAddtime()));
            billUserTV.setText(mOutputBill.getAddusername());
            billNumberTV.setText(mOutputBill.getObnum());
            piWeightTV.setText(String.valueOf(mOutputBill.getSendtareweight()));
            maoWeightTV.setText(String.valueOf(mOutputBill.getSendgrossweight()));
            netWeightTV.setText(String.valueOf(mOutputBill.getSendnetweight()));
            sendCompanyTV.setText(mOutputBill.getSendcompany());
            customerCompanyTV.setText(mOutputBill.getBuycompany());
            billCoalNameTV.setText(mOutputBill.getCoaltype());
            if (mOutputBill.getStatus() == 1) {
                statusNameTV.setText("已出场");
                outTimeTV.setText(TimeUtils.parseDateTime(mOutputBill.getOuttime()));
            } else {
                statusNameTV.setText("未出场");
                outTimeTV.setText("");
            }
        } else {
            billTimeTV.setText("");
            billUserTV.setText("");
            billNumberTV.setText("");
            piWeightTV.setText("0");
            maoWeightTV.setText("0");
            netWeightTV.setText("0");
            sendCompanyTV.setText("");
            customerCompanyTV.setText("");
            billCoalNameTV.setText("");
            statusNameTV.setText("未出场");
            outTimeTV.setText("");
        }
    }


    @Override
    public void showMessage(String msg) {
        Toast.makeText(LoadCarDetailActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(LoadCarDetailActivity.this).showDialog("",0);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(LoadCarDetailActivity.this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {
        if (result instanceof OutputBill) {
            mOutputBill = (OutputBill) result;
            bindView();
        } else {
            showMessage("OutputBill 类型错误 ");
        }
    }

    @Override
    public void onLoadMoreData(Object result) {

    }
}
